import {combineReducers} from 'redux-immutable';
import Immutable from 'immutable';
import * as actions from './actions';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION, DEFAULT_SUBMIT_DATA} from './constants';
const EMPTY_ARRAY = [];
const EMPTY_OBJECT = {};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(
                Immutable.fromJS({
                    isFetching: true,
                    message: ''
                })
            );
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(
                Immutable.fromJS({
                    isFetching: false,
                    message: '',
                    data: action.data
                })
            );
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(
                Immutable.fromJS({
                    isFetching: false,
                    message: action.message
                })
            );
        default:
            return state;
    }
};

const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_BEGIN:
            return state.merge({
                isFetching: true,
                message: ''
            });

        case actions.GET_INIT_SUCCESS:
            return state.merge({
                isFetching: false,
                brands: action.data.brands || [],
                isDealerUser: action.data.isDealerUser,
                message: ''
            });
        case actions.GET_INIT_FAIL:
            return state.merge({
                message: action.message,
                isFetching: false
            });

        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message || '',
                data: EMPTY_ARRAY
            });
        default:
            return state;
    }
};

const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.set(action.name, action.value);
        case actions.SELECT_DEALER_QUERY:
            return state.merge({
                ...action.data
            });
        case actions.RESET_QUERY_PANEL: {
            const {pageSize, sortField, isDesc} = state.toJS();
            return Immutable.fromJS({
                ...DEFAULT_QUERY_OPTION,
                pageSize,
                sortField,
                isDesc
            });
        }
        case actions.GET_LIST_SUCCESS:
            return state.withMutations(map =>
                map
                    .set('pageSize', action.condition.pageSize)
                    .set('sortField', action.condition.sortField)
                    .set('isDesc', action.condition.isDesc)
            );

        default:
            return state;
    }
};

const queryBySort = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};


const detail = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: EMPTY_OBJECT
            });

        case actions.GET_DETAIL_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data
            });

        case actions.CLEAR_DATA:
        case actions.GET_DETAIL_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message,
                data: EMPTY_OBJECT
            });

        default:
            return state;
    }
};


const submitData = (state, action) => {
    switch(action.type) {
        case actions.SET_DEFAULT_BRANDID: {
            if(state.get('brandId'))
                return state;
            return state.set('brandId', action.id);
        }
        case actions.SAVE_SUBMIT_CONDITION:
            return state.set(action.name, action.value);
        case actions.CLEAR_DATA:
            return Immutable.fromJS(DEFAULT_SUBMIT_DATA);
        case actions.SELECT_DATA:
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({...action.data});
        case actions.SELECT_DEALER: {
            return Immutable.fromJS(action.data);
        }
        case actions.UPDATE_ATTACHMENTS:
            return state.updateIn(['attachments'], obj => obj.push(Immutable.fromJS(action.data)));
        case actions.REMOVE_ATTACHMENTS: {
            const index = state.get('attachments').findIndex(item => item.get('fileId') === action.id);
            if(index === -1)
                return state;
            return state.updateIn(['attachments'], list => list.splice(index, 1));
        }
        case actions.UPDATE_PIATTACHMENTS:
            return state.updateIn(['piAttachments'], obj => obj.push(Immutable.fromJS(action.data)));
        case actions.REMOVE_PIATTACHMENTS: {
            const index = state.get('piAttachments').findIndex(item => item.get('fileId') === action.id);
            if(index === -1)
                return state;
            return state.updateIn(['piAttachments'], list => list.splice(index, 1));
        }
        case actions.UPDATE_DETAILS:
            return state.updateIn(['details'], list => list.concat(Immutable.fromJS(action.details)));
        case actions.DELEDT_DETAILS_ALL:
            return state.set('details', Immutable.List());
        case actions.DELEDT_DETAILS: {
            const index = state.get('details').findIndex(item => item.get('sourceId') === action.id);
            if(index === -1)
                return state;
            return state.updateIn(['details'], list => list.splice(index, 1));
        }
        case actions.SAVE_DETAILS: {
            const detailIndex = state.get('details').findIndex(item => item.get('sourceId') === action.data.sourceId);
            if(detailIndex === -1)
                return state;
            return state.updateIn(['details', detailIndex], obj => obj.merge({...action.data}));
        }
        default:
            return state;
    }
};

const domainData = combineReducers({
    initData,
    permission,
    list,
    detail,
});
const appState = combineReducers({
    queryCondition,
    queryBySort,
    submitData,
});
export default combineReducers({
    domainData,
    appState,
});
